package com.doupi.partner.service.impl;

import com.doupi.partner.dal.AdministratorPermissionMapper;
import com.doupi.partner.dal.model.AdministratorPermission;
import com.doupi.partner.service.AdministratorPermissionService;
import com.doupi.partner.service.AdministratorRoleHasPermissionService;
import com.doupi.partner.util.Pagination;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdministratorPermissionServiceImpl implements AdministratorPermissionService {
	private final AdministratorPermissionMapper administratorPermissionMapper;

	private final AdministratorRoleHasPermissionService administratorRoleHasPermissionService;

	@Autowired
	public AdministratorPermissionServiceImpl(AdministratorPermissionMapper administratorPermissionMapper, AdministratorRoleHasPermissionService administratorRoleHasPermissionService) {
		this.administratorPermissionMapper = administratorPermissionMapper;
		this.administratorRoleHasPermissionService = administratorRoleHasPermissionService;
	}

	public int deleteByPrimaryKey(Long id) {
		int flag = administratorPermissionMapper.deleteByPrimaryKey(id);
		if (flag > 0) {
			// 删除角色权限对应关系
			administratorRoleHasPermissionService.deleteByAdministratorPermissionId(id);
		}
		return flag;
	}

	public int insert(AdministratorPermission record) {
		return administratorPermissionMapper.insert(record);
	}

	public AdministratorPermission selectByPrimaryKey(Long id) {
		return administratorPermissionMapper.selectByPrimaryKey(id);
	}

	public int updateByPrimaryKeySelective(AdministratorPermission record) {
		return administratorPermissionMapper.updateByPrimaryKeySelective(record);
	}

	public int countAdministratorPermission(AdministratorPermission administratorPermission) {
		return administratorPermissionMapper.countAdministratorPermission(administratorPermission);
	}

	public List<AdministratorPermission> queryAdministratorPermissions(AdministratorPermission administratorPermission, RowBounds rowBounds) {
		return administratorPermissionMapper.queryAdministratorPermissions(administratorPermission, rowBounds);
	}

	public AdministratorPermission selectAdministratorPermissionForUpdateByPrimaryKey(Long id) {
		return administratorPermissionMapper.selectAdministratorPermissionForUpdateByPrimaryKey(id);
	}

	public Pagination<AdministratorPermission, AdministratorPermission> queryAdministratorPermissions(Pagination<AdministratorPermission, AdministratorPermission> pagination) {
		int count = countAdministratorPermission(pagination.getParam());
		pagination.setTotalCount(count);
		if (count > 0) {
			pagination.setRecords(queryAdministratorPermissions(pagination.getParam(), new RowBounds(pagination.getOffset(), pagination.getLimit())));
		}
		return pagination;
	}
}
